﻿@{
    ViewBag.Title = "Ins";
}
@section scripts
{   
    <script src="@Url.Content("~/Scripts/shared/share.js?v=")@ViewBag.version" type="text/javascript"></script> 
    <script src="@Url.Content("~/Scripts/shared/shown.js?v=")@ViewBag.version" type="text/javascript"></script>  
    <script src="@Url.Content("~/Scripts/Bill/Bill.js?v=")@ViewBag.version" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/Bill/scan.js?v=")@ViewBag.version" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/common/Print.js?v=")@ViewBag.version" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/zxx.drag.1.0.js")" type="text/javascript"></script>
    <link href="~/Scripts/miniui/themes/default/medium-mode.css" rel="stylesheet" type="text/css" />       
        <style type="text/css">
        .window-mask {
                position: absolute;
                left: 0;
                top: 0;
                width: 100%;
                height: 100%;
                filter: alpha(opacity=40);
                opacity: 0.40;
                font-size: 1px;
                overflow: hidden;
                display:none;
                background-color: gray;
            }
    </style>

}
<body>
   <form id="form1" runat="server" method="post" style="height: 100%">
    
    <div class="mini-toolbar" style="padding:2px;border-top:0;border-left:0;border-right:0;"> 
        <table width = "1000px;">
            <tr>
                <td width = "1000px;">
                    <a class="mini-button" id="new" name="new" iconCls="icon-new" onclick="NewBills" plain="true" visible="@ViewBag.save">新增</a>
                    <a class="mini-button" id="save" name="save" iconCls="icon-save" onclick="saveBill" plain="true" visible="@ViewBag.save">保存</a>
                    <a class="mini-button" id="addrow" name="addrow" iconCls="icon-addnew" onclick="addRow" plain="true" visible="@ViewBag.save">增行</a>
                    <a class="mini-button" id="delrow" name="delrow" iconCls="icon-remove" onclick="removeRow" plain="true" visible="@ViewBag.save">删行</a>
                    <a class="mini-button" id="import" name="import" iconCls="icon-redo" onclick="ExcelImport" plain="true" visible="@ViewBag.save">导入</a>
                    <a class="mini-button" id="modify" name="modify" iconCls="icon-edit" onclick="modifys" plain="true" visible="@ViewBag.save">改单</a>
                    <a class="mini-button" id="check" name="check" iconCls="icon-ok" onclick="checkBill" plain="true" visible="@ViewBag.check">审核</a>                    
                    <a class="mini-button" id="print" iconCls="icon-print" onclick="printBill" plain="true" visible="@ViewBag.print">打印</a>
                    <a class="mini-button" id="getdata" iconCls="icon-collapse" onclick="getBill" plain="true">浏览</a>
                    <span class="separator"></span>
                    <a class="mini-button" id="close" iconCls="icon-close" onclick="CloseWindowBill" plain="true">关闭</a>
                    <input id="ScanAdd" name="ScanAdd" class="mini-hidden"/>
                </td>
            </tr>
        </table>                 
    </div>
    
    <div id = "DivHeader">
        <table style="width:770px;">
            <tr>
                <td width ="80px" align="right">门店名称：</td>
                <td width ="190px">    
                    @Html.Partial("~/Views/Modular/LookupStockData.cshtml")                   
                </td>
                <td width ="80px" align="right">开单日期：</td>
                <td width ="215px">    
                    <input name="FDate" class="mini-datepicker" style="width:215px;"/>
                </td>
                 <td width ="80px" align="right">单据编号：</td>
                 <td width ="150px">    
                    <input name="FBillNo" class="mini-textbox" required="true" readonly="readonly"style="width:150px;"/>
                 </td>
            </tr>
            <tr>
                <td align="right">单据备注：</td>                    
                <td >  
                    <input name="FRemark" class="mini-textbox"  style="width:198px;"/>  
                </td>
                <td align="right">销售成本：</td>
                <td >
                    <input id="FUpPosFlag" name="FUpPosFlag" class="mini-combobox" style="width:90px;" showNullItem="false"
                      onvaluechanged="onChanged()" allowInput="false" align="left" value="0" data="comUpPos"/>
                    <input name="FStarTime" class="mini-datepicker" style="width:120px;"/>
                </td>
                <td align="right">单据状态：</td>
                <td >    
                    <input name="txtCheckFlag" class="mini-textbox" readonly="readonly" style="width:150px;"/>
                </td>
            </tr>
        </table>
    </div>
    <div class="mini-fit">
        <div id="datagrid1" class="mini-datagrid" style="width:100%;height:100%;" 
                showPager = "false" showPageSize="false" showPageIndex="false" skipReadOnlyCell="true"
                allowCellSelect="true" allowSortColumn = "false" allowCellEdit="true"showColumnsMenu="false"  
                editNextOnEnterKey="true"  editNextRowCell="false" 
                oncellcommitedit="onCellCommitEdit"  navEditMode="true"
                fitColumns="false">
                <div property="columns">            
                <div type="checkcolumn" >选择</div>      
                <div type="indexcolumn" width="35" headerAlign="center" >序号</div>
                <div field="mFNumber" name="mFNumber" displayField = "mFNumber" width="100" headerAlign="center">商品编码
                <input property="editor" class="mini-buttonedit" onbuttonclick = "onButtonEdit" onvaluechanged = "onValueChanged" style="width:100%;"/>
                </div>  
                <div field="mFName" name="mFName" width="120" headerAlign="center">商品名称</div>  
                <div field="mFModel" name="mFModel" width="100" headerAlign="center">规格型号</div>
                <div field="mFUnitName" name="mFUnitName" width="40" headerAlign="center">单位</div>
                <div field="mFPrice" name="mFPrice" width="80" numberFormat="#,0.00" headerAlign="center" align="right">原价
                </div>
                <div field="mFNewPrice" name="mFNewPrice" width="80" numberFormat="#,0.00" headerAlign="center" align="right">新价
                    <input property="editor" class="mini-spinner" maxValue="99999999" style="width:100%;"changeOnMousewheel ="false" showbutton="false" increment ="0"/>
                </div>
                <div field="mFDifPrice" name="mFDifPrice" numberFormat="#,0.00" width="80" headerAlign="center" align="right" summaryType="sum">差价</div>
                <div field="mFCode" name="mFCode" width="120" headerAlign="center">商品条码</div>
                <div field="mFRemark" name="mFRemark" width="150" headerAlign="center" >备注
                    <input property="editor" class="mini-textbox" style="width:100%;"/>
                </div>
            </div>
        </div>
    </div>

     @Html.Partial("~/Views/Modular/Bill/Footer.cshtml")
     @Html.Partial("~/Views/Modular/Bill/ScanWin.cshtml")

    <table style="width:770px;">
        <tr>
            <td width ="63px" align="right">提示：</td>
            <td width ="707px">成本调整单默认调整库存成本，如果要同时调整销售成本，请选择销售成本的调整开始日期，即“调整始于”。</td>
        </tr>
    </table>

    <script type="text/javascript">

        var comUpPos = [{ "id": "0", "text": "不调整" },
                           { "id": "1", "text": "调整始于" }]

        mini.parse();
        var form = new mini.Form("form1");
        var grid = mini.get("datagrid1");      
        var uID = parent.window.getSystemAppID();
        var uCode = parent.window.getSystemAppCode();
        var uName = parent.window.getSystemAppName();
        var LastField = "mFRemark";

        function ExtraFunc(obj) {
            obj.mFNewPrice = obj.mFPrice;
            obj.mFDifPrice = obj.mFPrice - obj.mFNewPrice;
            return obj;
        }

        $(function () {
            var oBox = document.getElementById("scan");
            var oBar = document.getElementById("title");
            if (id == 0)
                BillState(0);
            InitScan();
            startDrag(oBar, oBox);
            InitGrid('AdjCostData');
        });

        function NewBills() {
            NewBill();
            mini.getbyName("FBillNo").setValue("");            
            mini.getbyName("FRemark").setValue("");
            mini.getbyName("txtCheckFlag").setValue("");
        }

        var id = getQueryString("billid");
        if (id != null) {
            mini.getbyName("KInterID").setValue(id);
        }        
        var datatype = getQueryString("datatype");      

        LoadData();

        function LoadData() {
            var minterid = mini.getbyName("KInterID").value;
            $.ajax({
                url: '../Bill/GetBillData',
                data: { mSystemAppID: uID, mBillID: minterid, mDataType: datatype },
                dataType: 'text',
                async: false,
                success: function (text) {
                    var dataR = mini.decode(text);
                    grid.setData(dataR.datadetail);
                    if (dataR.datamaster.length > 0) {
                        var mdata = dataR.datamaster[0];
                        form.setData(mdata);
                        mini.getbyName("FStockID").setValue(mdata.FStockID);
                        mini.getbyName("FStockID").setText(mdata.FStockNumber);
                        
                        if (mdata.FCheckFlag == 1) {
                            BillState(2);
                            mini.get("import").setEnabled(false);
                        }
                        else {
                            BillState(1);
                            if (minterid == 0) {
                                mini.get("import").setEnabled(true);
                            }
                            else {
                                 mini.get("import").setEnabled(false);
                            }
                        }
                        if ((mdata.KInterID == "0") || (mdata.KInterID == null)) {
                            newBills();
                        }
                    }
                    else {
                        newBills();
                    }
                    onChanged(0);
                }
            });
        }

        function newBills() {
            NewBills();
            var nowDate = new Date();
            mini.getbyName("FBillDate").setValue(nowDate);
            mini.get("import").setEnabled(true);
            nowDate = nowDate.setDate(nowDate.getDate() + 1);
            nowDate = new Date(nowDate);
            mini.getbyName("FStarTime").setValue(nowDate);            
        }
        function modifys() {
            modify();
            mini.get("import").setEnabled(true);
        }

        function checkValid() {
            if ((mini.getbyName("FBillerID").getValue() == "") || (mini.getbyName("FBillerID").getValue() == "0")) {
                var newbillerid = parent.window.getSystemAppID();
                mini.getbyName("FBillerID").setValue(newbillerid);
            }

            var MainData = form.getData();
            if (MainData.FStockID.toString() == "") {
                mini.showTips({ content: "<b>保存失败</b> <br/>门店不能空值!", state: "danger", x: "center", y: "top", timeout: 3000 });
                return false;
            }
            return true;
        }


        function saveBill() {
            if (checkValid() == false) return;
            var MainData = form.getData();
            var cDate = mini.getbyName("FDate").getFormValue();
            var cStarTime = mini.getbyName("FStarTime").getFormValue();
            var rCnt = grid.getData().length;
            var mjson = "[{'KInterID':'" + MainData.KInterID + "',";
            mjson = mjson + "'FBillNo':'" + MainData.FBillNo + "',";
            mjson = mjson + "'FSystemAppID':'" + uID + "',";
            mjson = mjson + "'FStockID':'" + MainData.FStockID + "',";
            mjson = mjson + "'FStarTime':'" + cStarTime + "',";
            mjson = mjson + "'FUpPosFlag':'" + MainData.FUpPosFlag + "',";
            mjson = mjson + "'FDate':'" + cDate + "',";
            mjson = mjson + "'FDataType':'" + datatype + "',";
            mjson = mjson + "'FRemark':'" + MainData.FRemark + "',";            
            var json = "'goods_list':[";
            var datarow = 0;            
            for (var i = 0; i < rCnt; i++) {
                js = "{";
                if (datarow > 0) js = ",{";
                var rs = grid.getRow(i);
                if (typeof (rs.mFNumber) == "undefined") continue;
                js = js + '"mFItemID":' + rs.mFItemID + ",";
                var remark = typeof (rs.mFRemark) == "undefined" ? "" : rs.mFRemark;
                js = js + '"mFRemark":' + '"' + remark + '",';
                var mFNewPrice = typeof (rs.mFNewPrice) == "undefined" ? 0 : rs.mFNewPrice;
                js = js + '"mFNewPrice":' + mFNewPrice + ",";
                var price = typeof (rs.mFPrice) == "undefined" ? 0 : rs.mFPrice;
                js = js + '"mFPrice":' + price + ",";
                var mFDifPrice = typeof (rs.mFDifPrice) == "undefined" ? 0 : rs.mFDifPrice;
                js = js + '"mFDifPrice":' + mFDifPrice + ',';
                js = js + '"mFRowNo":0, "mFInterID":0';
                js = js + "}"
                json = json + js;
                datarow = datarow + 1;
            }
            if (mjson.length < 8) {
                mini.alert("没有要保存的数据！");
                return;
            }
            json = json + "]";
            mjson = mjson + json + "}]";
            grid.loading("保存中，请稍后......");
            $.ajax({
                url: '../Bill/SaveBillData_AdjCost',
                data: { mdatajson: mjson, FSystemAppID: parent.window.getSystemAppID() },
                type: "post",
                cache: false,
                success: function (txt) {
                    var ret = mini.decode(txt);
                    grid.unmask();
                    if (ret.id != -1) {
                        mini.getbyName("KInterID").setValue(ret.id);
                        mini.getbyName("FBillNo").setValue(ret.FBillNo);
                        MyBillState(1);
                        mini.showTips({ content: "<b>保存成功</b>", state: "success", x: "center", y: "top", timeout: 3000 });
                    }
                    else {
                        mini.alert("错误信息:" + ret.Status);
                    }
                },
                error: function (xhr) {
                    grid.unmask();
                    mini.alert("错误信息:" + xhr.responseText);
                }
            });
        }

        function onCellCommitEdit(e) {
            var row = e.record;
            var total = 0;
            var qty = 0;
            var alltotal = 0; //合计金额,总计
            var mdisc = 100;
            if ((typeof (row.mFName) == "undefined") || (row.mFName == "")) {
                e.cancel = true;
                return;
            }
            if (typeof (row.mFNumber) == "undefined") {
                return;
            }
            mini.get("save").setEnabled(true);//编辑后保存按钮可用

            if (e.field == "mFNewPrice") {
                total = (e.value - row.mFPrice);
                total = total.toFixed(2);
                grid.updateRow(row, { mFDifPrice: total });
            }
        }

        function MyBillState(flag) {
            switch (flag) {
                case 0:   //新增
                    mini.get("import").setEnabled(true);
                    break;
                case 1:   //保存
                    mini.get("import").setEnabled(false);
                    break;
                case 2:   //审核
                    mini.get("import").setEnabled(false);
                    break;
                case 3:   //关帐
                    mini.get("import").setEnabled(false);
                    break;
                case 4:   //修改后
                    mini.get("import").setEnabled(true);
                    break;
            }
            BillState(flag);
        }

        function onChanged(value) {
            var vs = value == undefined ? mini.get("FUpPosFlag").getValue() : value;
            if (vs == 0) {                
                mini.getbyName("FStarTime").setEnabled(false);
            }
            else
                mini.getbyName("FStarTime").setEnabled(true);
        }

        function getBill() {
            mini.open({
                url: "../Bill/FindAdjPrice",
                title: "查看单据", width: 900, height: 530,
                onload: function () {
                    var iframe = this.getIFrameEl();
                    var data = { BillKind: datatype };
                    iframe.contentWindow.SetData(data);
                },
                ondestroy: function (action) {
                    if (action) {
                        if ((action.KInterID == null) || (action.KInterID == "undefined")) return;
                        mini.getbyName("KInterID").setValue(action.KInterID);
                        LoadData();
                    }
                }
            });
        }

        function printBill() {
            var url = "../Report/PrintReport";
            mini.open({
                url: url,
                title: "打印", width: 1000, height: 700,
                onload: function () {
                    var iframe = this.getIFrameEl();
                    var NoneDisplayCol = [0];
                    iframe.contentWindow.InitalPrint(grid, NoneDisplayCol, "成本调整单");
                }
            });
        }

        function ExcelImport() {
            var FSystemAppID = parent.window.getSystemAppID();
            mini.open({
                url: "../Bill/ImportData?mDataType=24&mSystemAppID=" + FSystemAppID + "&FStockID=" + mini.getbyName("FStockID").getValue(),
                title: "调价Excel导入", width: 450, height: 200,
                ondestroy: function (action) {
                    if (action == "close")
                        return;
                    if (action) {
                        if (action.length > 0) {
                            if (grid.getData().length > 0) {
                                if (grid.getData()[grid.getData().length - 1].mFNumber == undefined) {
                                    var row = grid.getRow(grid.getData().length - 1);
                                    grid.removeRow(row, false);
                                }
                            }
                            for (var i = 0, l = action.length; i < l; i++) {
                                var rw = action[i];
                                var newRow = {
                                    mFItemID: rw.mFItemID, mFNumber: rw.mFNumber, mFName: rw.mFName, mFModel: rw.mFModel, mFUnitName: rw.mFUnitName,
                                    mFPrice: rw.mFPrice, mFNewPrice: rw.mFPrice, mFCode: rw.mFCode, mFDifPrice: rw.mFPrice - rw.mFPrice
                                };
                                grid.addRow(newRow, grid.getData().length);
                            }
                            MyBillState(4);
                        }
                    }
                }
            });
        }

    </script>

    </form>
</body>